import { HttpParams } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd/message';
import { SshServer } from 'src/Model/SshServer';
import { AjaxServiceService } from 'src/services/AjaxService.service';

@Component({
  selector: 'app-Ssh',
  templateUrl: './Ssh.component.html',
  styleUrls: ['./Ssh.component.css']
})
export class SshComponent implements OnInit {
  visible = false;
  //变量
  listOfCurrentPageData: SshServer[] = [];
  //表格相关
  total = 0;
  pageindex = 1;
  pagesize = 10;
  tableloading = false;



  //drawer
  item = new SshServer();

  isModelVisible=false;
  shellcontent="";
  shellresult="";
  handleitem= new SshServer();

  okloading=false;
  constructor(private ajax: AjaxServiceService, private message: NzMessageService, private router: Router) { }

  ngOnInit() {
    this.searchData();
  }

  ngAfterViewInit(): void {
    //Called after ngAfterContentInit when the component's view has been initialized. Applies to components only.
    //Add 'implements AfterViewInit' to the class.

  }

  loginout(): void {
    localStorage.removeItem("Authorization");
    this.router.navigate(["/login"]);
  }
  taskpage(): void {
    this.router.navigate(["/home"]);
  }
  newSshServer(): void {
      this.item=new SshServer();
      this.visible=true;
  }
  update(id: string | number | boolean): void {
      let params=new HttpParams();
      params=params.append("ID",id);
      this.ajax.get("/SshServer/GetOne",params).then((res:any)=>{
          this.item=res.data;
          this.visible=true;
      });
  }
  searchData(reset: boolean = false) {
    if (reset) {
      this.pageindex = 1;
    }
    this.tableloading = true;
    let params = new HttpParams();
    params = params.append("PageIndex", this.pageindex.toString());
    params = params.append("PageSize", this.pagesize.toString());
    this.ajax.get("/SshServer/List", params).then((res: any) => {
      this.tableloading = false;
      this.total = res.data.total;
      this.listOfCurrentPageData = res.data.rows;

    });
  }
  deleteOne(ID: string | number | boolean): void {
    let params=new HttpParams();
    params=params.append("ID",ID);
    this.ajax.get("/SshServer/DeleteOne",params).then((res:any)=>{
      if (res.code == 0){
        this.searchData();
      }else{
        this.message.error(res.msg);
      }

    });
  }
  close(): void {
    this.visible = false;
  }
  AddOrEditSsh():void{
    this.ajax.post("/SshServer/AddOrUpdate", this.item).then((res: any) => {
      if (res.code == 0) {
        this.message.success("提交成功！");
        this.close();
        this.searchData();
      } else {
        this.message.error(res.msg);
      }
    })

  }

  handleCancel():void{

    this.isModelVisible=false;
  }

  handleOk():void{
    this.okloading=true;
    this.handleitem.shell=this.shellcontent;
    this.ajax.post("/SshServer/ExceShell",this.handleitem).then((res:any)=>{
        this.shellresult=res.data;
        this.okloading=false;
    })
  }

  execshell(item:SshServer):void{
    this.handleitem=item;
    this.shellcontent="";
    this.isModelVisible=true;
  }
}
